<?php
/**
 * LilBlogs plugin
 * This is a plugin which handles common posts and comments scenario
 *
 * @copyright     Copyright (c) 2010, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_blogs_comments_plugin
 * @subpackage    lil_blogs_comments_plugin.controllers
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Controller', 'Lil.LilPlugin');
/**
 * LilBlogsCommentsPluginController class
 *
 * @uses          LilBlogsPluginController
 * @package       lil_blogs_plugin
 * @subpackage    lil_blogs_plugin.controllers
 */
class LilBlogsCommentsPluginController extends LilPluginController {
	var $name = 'LilBlogsCommentsPlugin';
/**
 * handlers property
 *
 * @var array
 * @access public
 */
	var $handlers = array(
		'admin_sidebar' => array('function' => '_setAdminSidebar', 'params' => array()),
		'admin_index_posts' => array('function' => '_addCommentCount', 'params' => array()),
		
		'admin_edit_post' => array('function' => '_addCommentFields', 'params' => array()),
		'admin_add_post' => array('function' => '_addCommentFields', 'params' => array()),
		
		'before_render' => array('function' => '_saveComment', 'params' => array()),
		
		'before_construct_model' => array('function' => '_associateComments', 'params' => array()),
	);
/**
 * _associateComments method
 *
 * @param mixed $model
 * @access public
 * @return void
 */
	function _associateComments($model) {
		if ($model->name == 'Post') {
			$model->hasMany['Comment'] = array(
				'className' => 'LilBlogsComments.Comment'
			);
		}
		return true;
	}
/**
 * _saveComment method
 *
 * Save comment from post/view
 *
 * @param mixed $controller
 * @access public
 * @return void
 */
	function _saveComment($controller) {
		if ($controller->name == 'Posts' && $controller->action == 'view' && !empty($controller->data)) {
			$Comment =& ClassRegistry::init('LilBlogsComments.Comment');
			if ($data = $Comment->add($controller->data)) {
				
				$data['Comment']['id'] = $controller->data['Comment']['id'] = $Comment->id;
				$controller->callPluginHandlers('after_save_comment', array('data' => $data));
			
				// notify user
				if ($data['Comment']['status'] > 0) {
					$this->Session->setFlash(__d('lil_blogs_comments', 'Your comment has been successfully saved.', true));
				} else {
					$this->Session->setFlash(__d('lil_blogs_comments', 'Your comment has gone into moderation.', true));
				}
			
				// redirect to self
				$this->redirect(Router::url(null, true));
			
			} else {
				$this->Session->setFlash(__d('lil_blogs_comments', 'Uh oh, we weren\'t able to save your comment.', true), 'error');
			}
		}
		return true;
	}
/**
 * _addCommentFields method
 *
 * Add comment fields
 *
 * @param mixed $view
 * @param mixed $form
 * @access public
 * @return void
 */
	function _addCommentFields($view, $form) {
		App::import('Helper', 'Form');
		$Form = new FormHelper();
		
		$comments_checkbox = array(
			'class'      => 'this->Form',
			'method'     => 'input',
			'parameters' => array(
				'field'   => 'Post.allow_comments',
				'options' => array(
					'type'  => 'checkbox',
					'label' => __d('lil_blogs_comments', 'Allow Comments', true)
				)
			)
		);
		
		$this->insertIntoArray(
			$form['form']['lines'], 
			array('allow_comments' => $comments_checkbox), 
			array('after' => 'body')
		);
					
		return $form;
	}
/**
 * _addCommentCount method
 *
 * Add comment count column
 *
 * @param mixed $view
 * @param mixed $index
 * @access public
 * @return void
 */
	function _addCommentCount($view, $index) {
		App::import('Helper', 'Html');
		$Html = new HtmlHelper();
		
		$index['table']['element']['head']['rows'][0]['columns']['comments'] = array(
			'html' => '&nbsp;'
		);
		
		foreach ($index['table']['element']['body']['rows'] as $k => &$row) {
			$row['columns']['comments'] = array(
				'parameters' => array('class' => 'center'),
				'html' => $Html->link(
					$row['data']['Post']['no_comments'],
					array(
						'admin'      => true,
						'plugin'     => 'lil_blogs_comments',
						'controller' => 'comments', 
						'action'     => 'index',
						'post_id'    => $row['data']['Post']['id'],
						'status'     => 'approved'
					), 
					array(
						'title' => __d('lil_blogs_comments', 'Comments', true), 
						'class' => 'PostsIndexCommentCount'
					),
					null, false
				)
			);
		}
		
		$index['table']['element']['foot']['rows'][0]['columns']['paginator']['parameters']['colspan']++;
		
		return $index;
	}
/**
 * _setAdminSidebar method
 *
 * Add admin sidebar elements.
 *
 * @param mixed $controller
 * @param mixed $sidebar
 * @access public
 * @return void
 */
	function _setAdminSidebar($controller, $sidebar) {
		$Comment =& ClassRegistry::init('LilBlogsComments.Comment');
		
		$sidebar['blogs']['items']['comments'] = array(
			'visible' => true,
			'title' => __d('lil_blogs_comments', 'Comments', true),
			'url'   => array(
				'plugin'     => 'lil_blogs_comments',
				'controller' => 'comments',
				'action'     => 'index',
				'admin'      => true,
			),
			'params' => array(),
			'active' => in_array($this->params['controller'], array('comments')),
			'expand' => $this->params['controller'] == 'comments',
			'submenu' => array(
				'all' => array(
					'visible' => true,
					'title' => 
						sprintf(
							__d('lil_blogs_comments', 'All (%d)', true),
							$Comment->find('count', array(
								'conditions' => array_merge(
									empty($this->params['named']['post_id']) ? array() : array('Comment.post_id' => $this->params['named']['post_id']),
									array('Post.blog_id' => $this->getAdminAreaId())
								),
								'contain'    => array('Post'),
							))
						),
					'url'   => array_merge(
						array(
							'plugin'     => 'lil_blogs_comments',
							'admin'      => true,
							'controller' => 'comments',
							'action'     => 'index',
						),
						empty($this->params['named']['post_id']) ? array() : array('post_id' => $this->params['named']['post_id'])
					),
					'params' => array(),
					'active' => (!isset($this->params['named']['status']))
				),
				'pending' => array(
					'visible' => true,
					'title' =>
						sprintf(
							__d('lil_blogs_comments', 'Pending (%d)', true),
							$Comment->find('count', array(
								'conditions' => array_merge(
									empty($this->params['named']['post_id']) ? array() : array('Comment.post_id' => $this->params['named']['post_id']),
									array(
										'Post.blog_id'   => $this->getAdminAreaId(),
										'Comment.status' => LILCOMMENT_PENDING
									)
								),
								'contain'    => array('Post'),
							))
						),
					'url'   => array_merge(
						array(
							'plugin'     => 'lil_blogs_comments',
							'admin'      => true,
							'controller' => 'comments',
							'action'     => 'index',
							'status'     => 'pending'
						),
						empty($this->params['named']['post_id']) ? array() : array('post_id' => $this->params['named']['post_id'])
					),
					'params' => array(),
					'active' => (isset($this->params['named']['status']) && ($this->params['named']['status'] == 'pending'))
				),
				'approved' => array(
					'visible' => true,
					'title' =>
						sprintf(
							__d('lil_blogs_comments', 'Approved (%d)', true),
							$Comment->find('count', array(
								'conditions' => array_merge(
									empty($this->params['named']['post_id']) ? array() : array('Comment.post_id' => $this->params['named']['post_id']),
									array(
										'Post.blog_id'   => $this->getAdminAreaId(),
										'Comment.status' => LILCOMMENT_APPROVED
									)
								),
								'contain'    => array('Post'),
							))
						),
					'url'   => array_merge(
						array(
							'plugin'     => 'lil_blogs_comments',
							'admin'      => true,
							'controller' => 'comments',
							'action'     => 'index',
							'status'     => 'approved'
						),
						empty($this->params['named']['post_id']) ? array() : array('post_id' => $this->params['named']['post_id'])
					),
					'params' => array(),
					'active' => (isset($this->params['named']['status']) && ($this->params['named']['status'] == 'approved'))
				),
			)
		);
		
		return $sidebar;
	}
}
?>